package com.origin.src.mq;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeConcurrentlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerConcurrently;
import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;
import org.apache.rocketmq.remoting.exception.RemotingException;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class RocketMQTest {

    public static void main(String[] args) throws InterruptedException {




        for (int i = 0; i < 10; i++) {
            int t = i;
            Thread.sleep(200);
            new Thread(() -> {
                try {
                    send("Topic" + t);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
            new Thread(() -> {
                try {
                    consumer("Topic" + t);
                } catch (MQClientException e) {
                    e.printStackTrace();
                }
            }).start();
        }


    }


    public static void send(String topic) throws MQClientException, MQBrokerException, RemotingException, InterruptedException {

        DefaultMQProducer producer = new DefaultMQProducer("MsgGroup");
        producer.setNamesrvAddr("127.0.0.1:9876;127.0.0.1:9877");
        producer.start();

        for (int i = 0; i < 1000; i++) {
            Message msg = new Message(topic,
                    ("Message" + i).getBytes(StandardCharsets.UTF_8));
            SendResult sendResult = producer.send(msg);
            System.out.printf("%s%n", sendResult);
        }

        producer.shutdown();
    }

    public static void consumer(String topic) throws MQClientException {
        DefaultMQPushConsumer defaultMQPushConsumer = new DefaultMQPushConsumer("MsgGroup");
        defaultMQPushConsumer.setNamesrvAddr("127.0.0.1:9876;127.0.0.1:9877");
        defaultMQPushConsumer.subscribe(topic, "*");

        defaultMQPushConsumer.registerMessageListener(new MessageListenerConcurrently() {

            @Override
            public ConsumeConcurrentlyStatus consumeMessage(
                    List<MessageExt> msgs,
                    ConsumeConcurrentlyContext consumeConcurrentlyContext) {

                StringBuilder sb = new StringBuilder();

                for (int i = 0; i < msgs.size(); i++) {
                    sb.append(new String(msgs.get(i).getBody()));
                    sb.append(" ");
                }
                System.err.printf("%s Receive New Messages: %s %n", Thread.currentThread().getName(), sb);

                return ConsumeConcurrentlyStatus.CONSUME_SUCCESS;
            }
        });
        defaultMQPushConsumer.start();
        System.out.printf("Consumer Started.%n");

    }


}
